﻿using UnityEngine;
using System.Collections;

public class ArmorySell : MonoBehaviour {

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
	}

    void OnClick() 
    {
        GameObject.Find("MainCamera").GetComponent<AudioSource>().PlayOneShot(Resources.Load<AudioClip>("Audio/Pencil_Check"));

        if (transform.parent.GetComponent<ArmoryPaper>().m_SelectedWeapon != null)
        {
            bool bRemover = true; // 한번만 지우게
            bool bLastCheck = true; // 마지막 한개남앗는지 확인
            int iDelIndex = 0;

            for (int i = 0; i < GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().ArmoryWeaponList.Count; ++i)
            {
                if (GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().ArmoryWeaponList[i] == transform.parent.GetComponent<ArmoryPaper>().m_SelectedWeapon && bRemover)
                {
                    iDelIndex = i;
                    bRemover = false;
                }
                else if (GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().ArmoryWeaponList[i] == transform.parent.GetComponent<ArmoryPaper>().m_SelectedWeapon && !bRemover)
                    bLastCheck = false;
            }

            GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().ArmoryWeaponList.RemoveAt(iDelIndex);

            GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().g_iMoney += (transform.parent.GetComponent<ArmoryPaper>().m_SelectedWeapon.m_iPrice / 2);
            transform.parent.GetComponent<ArmoryPaper>().SceneWpnCategorize();
            transform.parent.Find("ScrollBar").GetComponent<ArmoryScrollBar>().ScrollBarReset();

            if (bLastCheck)
            {
                transform.parent.GetComponent<ArmoryPaper>().m_SelectedWeapon = null;
                GameObject.Find("SelectedArm").GetComponent<UISprite>().enabled = false;
                transform.Find("ArmorySellMoney").gameObject.SetActive(false);
            }
        }
    }

    void OnHover(bool isOver)
    {
        if (transform.parent.GetComponent<ArmoryPaper>().m_SelectedWeapon != null)
        {
            if (isOver)
            {
                transform.Find("ArmorySellMoney").gameObject.SetActive(true);
                transform.Find("ArmorySellMoney").GetComponent<ArmorySellMoney>().BoxSetting(0, transform.parent.gameObject, new Vector3(0.1f, 0.1f),
                   "", string.Format("$ {0}", transform.parent.GetComponent<ArmoryPaper>().m_SelectedWeapon.m_iPrice/2));
            }
            else
            {
                transform.Find("ArmorySellMoney").gameObject.SetActive(false);
            }
        }
    }
}
